#include "base/core/com_define.h"
#include "base/core/double_list.h"

void DoubleList::Insert(DoubleNode* prev, DoubleNode* elem)
{
	MMO_ASSERT(prev);
	MMO_ASSERT(elem);
	DoubleNode* next = prev->next;
	prev->next = elem;
	elem->prev = prev;
	elem->next = next;
	if (next)
	{
		next->prev = elem;
	}
}

bool DoubleList::Delete(DoubleNode* elem)
{
	bool result = false;
	MMOLOG_ASSERT_EXIT(elem);
	MMO_PROCESS_SUCCESS(head_ == NULL);
	if (head_ == elem) 
	{
		head_ = head_->next;
		if (head_ == NULL) 
		{
			tail_ = NULL;
		}
		else 
		{
			head_->prev = NULL;
		}
		MMO_ASSERT(count_ > 0);
		--count_;
		elem->ResetNode();
	}
	else if (tail_ == elem) 
	{
		tail_ = tail_->prev;
		tail_->next = NULL;
		MMO_ASSERT(count_ > 0);
		--count_;
		elem->ResetNode();
	}
	else if (elem->prev != NULL && elem->next != NULL) 
	{
		elem->prev->next = elem->next;
		elem->next->prev = elem->prev;
		MMO_ASSERT(count_ > 0);
		--count_;
		elem->ResetNode();
	}
Exit1:
	result = true;
Exit0:
	return result;
}
